package com.zmn.oms.dubbo.impl.channel;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.oms.business.interfaces.conf.channel.ConfOrderChannelBService;
import com.zmn.oms.common.dro.conf.channel.ConfChannelWorkOrderTypeDRO;
import com.zmn.oms.common.dro.conf.channel.ConfOrderChannelDRO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.channel.ConfOrderChannelListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.conf.channel.ConfOrderChannel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述: 渠道订单参数配置 Dubbo接口实现类
 *
 * @author: liudi
 * @since: 2022/02/22 16:42
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 20000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ConfOrderChannelListRemoteServiceImpl implements ConfOrderChannelListRemoteService {

	@Resource
	private ConfOrderChannelBService confOrderChannelBService;

	@Override
	public ResponseDTO<ConfOrderChannelDRO> getByChannelId(Integer channelId) {
		log.info("ConfOrderChannelRemoteServiceImpl#getByChannelId 参数 channelId：{}", channelId);
		try {
			ConfOrderChannelDRO confOrderChannelDRO = confOrderChannelBService.getByChannelId(channelId);
			if (Objects.isNull(confOrderChannelDRO)) {
				return ResponseDTO.fail("渠道订单配置不存在");
			}
			return ResponseDTO.success(confOrderChannelDRO, "OK");
		} catch (Exception e) {
			return ResponseDTO.fail(e.getMessage());
		}
	}

	@Override
	public ResponseDTO<List<ConfOrderChannelDRO>> listConfOrderChannelByChannelIds(List<Integer> channelIds) {
		log.info("根据渠道ID集合查询渠道订单配置 参数 channelIds：{}", channelIds);
		try {

			List<ConfOrderChannel> confOrderChannelList = confOrderChannelBService.listConfOrderChannelByChannelIds(channelIds);
			if (CollectionUtils.isEmpty(confOrderChannelList)) {
				return ResponseDTO.success(Lists.newArrayList());
			}

			List<ConfOrderChannelDRO> confOrderChannelDROList = BeanMapper.mapList(confOrderChannelList, ConfOrderChannelDRO.class);
			return ResponseDTO.success(confOrderChannelDROList);
		} catch (Exception e) {
			return ResponseDTO.fail(e.getMessage());
		}
	}

	@Override
	public ResponseDTO<ConfChannelWorkOrderTypeDRO> getWorkOrderTypeByChannelId(Integer channelId) {
		log.info("ConfOrderChannelRemoteServiceImpl#getWorkOrderTypeByChannelId 参数 channelId：{}", channelId);
		try {
			ConfChannelWorkOrderTypeDRO result = confOrderChannelBService.getWorkOrderTypeByChannelId(channelId);
			if (Objects.isNull(result)) {
				return ResponseDTO.fail("渠道订单配置不存在");
			}
			return ResponseDTO.success(result);
		} catch (Exception e) {
			log.error("getWorkOrderTypeByChannelId参数channelId：{}，异常：", channelId, e);
			return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
		}
	}


	@Override
	@ReadOnlyConnection
	public ResponseDTO<Boolean> checkNotifyEngineer(Integer channelId) {
		log.info("checkNotifyEngineer 入参 channelId：{}", channelId);
		try {
			boolean flag = confOrderChannelBService.checkNotifyEngineer(channelId);
			return ResponseDTO.success(flag);
		} catch (Exception e) {
			log.error("error：", e);
			return ResponseDTO.fail("系统错误! 调用失败!");
		}
	}

}
